package com.zzp.basepresenter.util;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.RelativeLayout;

public class LayoutUtil {
    private static LayoutUtil instance;

    public static LayoutUtil getInstance() {
        if (instance == null) {
            synchronized (LayoutUtil.class) {
                if (instance == null) {
                    instance = new LayoutUtil();
                }
            }
        }
        return instance;
    }

    /**
     * 获得状态栏的高度
     *
     * @param context
     * @return
     */
    public int getStatusHeight(Context context) {
        int result = 0;
        //获取状态栏高度的资源id
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }


    /**
     * 设置布局最顶部控件距离屏幕顶端属性
     *
     * @param context      上下文
     * @return
     */
    public RelativeLayout.LayoutParams getRelativeLayoutParams(Activity context, View view) {
        RelativeLayout.LayoutParams barParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
        barParams.topMargin = getStatusHeight(context);
        return barParams;
    }


    /**
     * 设置布局最顶部控件距离屏幕顶端属性
     *
     * @param context      上下文
     * @return
     */
    public RelativeLayout.LayoutParams getRelativeLayoutMarginParams(Activity context, View view) {
        RelativeLayout.LayoutParams barParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
        int topMargin = barParams.topMargin;
        barParams.topMargin = getStatusHeight(context) + topMargin;
        return barParams;
    }
}
